﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraTreeList.Nodes;
using ESOL.Business.Sec;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Sec
{
    public partial class frmMaintainRoleSetting : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public SecRoleEntity RoleEntity { get; set; }
        public SecMenuEntity MenuEntity { get;set ; }
        public SecObjectEntity ObjectEntity { get; set; }//+
        #endregion

        #region Constructor
        public frmMaintainRoleSetting()
        {
            InitializeComponent();
            ThisActionType = ActionType.GrandRole;
        }
        #endregion

        #region Functions
        private void BindTreeRoleUsers()
        {
            List<SecUserEntity> list = SecUserBs.GetAllAvailableUser();
            treeRoleUsers.DataSource = list;
            treeRoleUsers.ExpandAll();
        }

        private void BindTreeRoleMenuObject()
        {
            DataTable dt = SecMenuBs.GetAllDsMenus();
            DataTable dtCopy = dt.Copy();
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ObjectType"].Equals("21"))
                {
                    continue;
                }
                List<SecObjectEntity> list = SecObjectBs.GetSecObjectByMenuID(dr["Id"].ToString());
                if (list == null || list.Count == 0)
                {
                    continue;
                }
                foreach (SecObjectEntity m in list)
                {
                    DataRow drCopy = dtCopy.NewRow();
                    drCopy["Id"] = m.ObjectId;
                    drCopy["ParentId"] = dr["Id"].ToString();
                    drCopy["ObjectType"] = "21";//按钮对象
                    drCopy["Name"] = m.ObjectName;
                    dtCopy.Rows.Add(drCopy);
                }
            }
            treeRoleMenuObject.DataSource = dtCopy;
            treeRoleMenuObject.ExpandAll();
        }
        
        private void SetFields()
        {
            if (RoleEntity == null)
            {
                return;
            }
            txtRoleName.Text = RoleEntity.RoleName;
            List<SecUserRoleEntity> listUser = SecUserRoleBs.GetSecUserRolesByRoleID(RoleEntity.RoleId);
            List<SecRoleMenuEntity> listMenu = SecRoleMenuBs.GetRoleMenusByRoleID(RoleEntity.RoleId);
            List<SecObjectRoleEntity> listObject = SecObjectRoleBs.GetObjectRoleByRoleID(RoleEntity.RoleId);
            List<SecUserEntity> userRoles = treeRoleUsers.DataSource as List<SecUserEntity>;
            SetTreeUserRoleChecked(listUser, userRoles);
            SetTreeRoleMenuObjectChecked(listMenu, listObject, treeRoleMenuObject.Nodes);
        }

        private void SetTreeUserRoleChecked(List<SecUserRoleEntity> list, List<SecUserEntity> userRoles)
        {
            if (list == null || userRoles == null || userRoles.Count == 0)
            {
                return;
            }
            int nIndex = 0;
            foreach (SecUserEntity userRole in userRoles)
            {
                if (IsUserInList(userRole.UserId, list))
                {
                    treeRoleUsers.Nodes[nIndex].Checked = true;
                }
                nIndex++;
            }
        }

        private bool IsUserInList(string userID, List<SecUserRoleEntity> list)
        {
            if (list == null)
            {
                return false;
            }
      
            foreach (SecUserRoleEntity entity in list)
            {
                if (entity.UserId == userID)
                {
                    return true;
                }
            }
            return false;
        }

        private void SetTreeRoleMenuObjectChecked(List<SecRoleMenuEntity> listMenu, List<SecObjectRoleEntity> listObject, TreeListNodes treeListNodes)
        {
            if (treeListNodes == null || treeListNodes.Count == 0)
            {
                return;
            }
                 
            foreach (TreeListNode treeListNode in treeListNodes)
            {
                if (treeListNode["ObjectType"].ToString().Equals("21"))
                {
                    if (IsObjectInList(treeListNode["Id"].ToString(), listObject) == true)
                    {
                        treeListNode.Checked = true;
                    }
                    else
                    {
                        treeListNode.Checked = false;
                    }
                }
                else
                {
                    if (IsMenuInList(treeListNode["Id"].ToString(), listMenu) == true)
                    {
                        treeListNode.Checked = true;
                    }
                    else
                    {
                        treeListNode.Checked = false;
                    }
                }
                if(treeListNode.HasChildren)
                    SetTreeRoleMenuObjectChecked(listMenu, listObject, treeListNode.Nodes);
            }
        }

        private bool IsMenuInList(string menuID, List<SecRoleMenuEntity> list)
        {
            if (list == null)
            {
                return false;
            }
            foreach (SecRoleMenuEntity entity in list)
            {
                if (entity.MenuId == menuID)
                {
                    return true;
                }
            }
            return false ;
        }

        private bool IsObjectInList(string objectID, List<SecObjectRoleEntity> list)
        {
            if (list == null)
            {
                return false;
            }
            foreach (SecObjectRoleEntity entity in list)
            {
                if (entity.ObjectId == objectID)
                {
                    return true;
                }
            }
            return false ;
        }

        private List<SecUserRoleEntity> GetUserRoleList()
        {
            List<SecUserRoleEntity> list = new List<SecUserRoleEntity>();
            foreach (TreeListNode node in treeRoleUsers.Nodes)
            {
                if (node.Checked)
                {
                    SecUserRoleEntity entity = new SecUserRoleEntity();
                    entity.RoleId = RoleEntity.RoleId;
                    entity.UserId = node.GetValue("UserId").ToString();
                    list.Add(entity);
                }
            }
            return list;
        }

        private void GetObjectRoleList(TreeListNodes nodes, List<SecRoleMenuEntity> menuRoles, List<SecObjectRoleEntity> objectRoles)
        {
            if (nodes == null || nodes.Count == 0)
            {
                return;
            }
            foreach (TreeListNode treeListNode in nodes)
            {
                string objectId = treeListNode.GetValue("Id").ToString();//+
                if (treeListNode .Checked ==true &&treeListNode.HasChildren==true)//+
                {
                    SecRoleMenuEntity secRoleMenu = new SecRoleMenuEntity();
                    //SecObjectRoleEntity secObject=new SecObjectRoleEntity ();//+
                    secRoleMenu.MenuId = treeListNode.GetValue("Id").ToString();
                    //secObject.ObjectId = treeListNode.GetValue("Id").ToString();//+
                    secRoleMenu.RoleId = RoleEntity.RoleId;
                    //secObject.ObjectId = ObjectEntity.ObjectId;//+
                    menuRoles.Add(secRoleMenu);
                    //objectRoles.Add(secObject);
                }
                else if (treeListNode.Checked == true && treeListNode.HasChildren == false)
                {
                    SecObjectRoleEntity secObjectRole = new SecObjectRoleEntity();
                    secObjectRole.RoleId = RoleEntity.RoleId;
                    secObjectRole.ObjectId = treeListNode.GetValue("Id").ToString();
                    objectRoles.Add(secObjectRole);
                }
                GetObjectRoleList(treeListNode.Nodes, menuRoles, objectRoles);
            }
        }

        private bool Save()
        {
            try
            {
                List<SecUserRoleEntity> listUser = GetUserRoleList();
                List<SecRoleMenuEntity> listMenu = new List<SecRoleMenuEntity>();
                List<SecObjectRoleEntity> listObject = new List<SecObjectRoleEntity>();
                GetObjectRoleList(treeRoleMenuObject.Nodes, listMenu, listObject);
                return SecRoleBs.RoleSetting(RoleEntity, listUser, listMenu, listObject);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        #endregion

        #region Events
        private void frmMaintainRoleSetting_Load(object sender, EventArgs e)
        {
            BindTreeRoleUsers();
            BindTreeRoleMenuObject();
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        #endregion
    }
}
